﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using QLTV.Public;


namespace QLTV.DAL
{
    public class DocGiaDAL
    {
        ConnectionData connect;
        // load danh sách độc giả
        public DataTable LoadDSDG()
        {
            try
            {
                connect = new ConnectionData();

                return connect.GetData("DSDG_Load");
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
        // Xóa độc giả
        public int XoaDG_MaDG(DocGiaDTO dgDTO)
        {
            try
            {
                connect = new ConnectionData();
                int Nparamater = 1;
                string[] name = new string[Nparamater];
                object[] values = new object[Nparamater];
                name[0] = "@MaDG";
                values[0] = dgDTO.MaDG;
                return connect.UpdateData("Delete_DocGia_ByMaDG", name, values, Nparamater);
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
        // Thêm độc giả
        public int InsertDG(DocGiaDTO dgDTO)
        {
            connect = new ConnectionData();
            try
            {
                //var binding = BindingObject(dgDTO);
                // return connect.UpdateData("sp_DocGia_Insert", binding.Names, binding.Values, binding.Names.Length);
                int Nparamater = 9;
                String[] name = new string[Nparamater];
                object[] values = new object[Nparamater];
                name[0] = "@MaDG";
                values[0] = dgDTO.MaDG;
                name[1] = "@HoVaTen";
                values[1] = dgDTO.HoVaTen;
                name[2] = "@GioiTinh";
                values[2] = dgDTO.GioiTinh;
                name[3] = "@NgaySinh";
                values[3] = dgDTO.NgaySinh;
                name[4] = "@DiaChi";
                values[4] = dgDTO.DiaChi;
                name[5] = "@Email";
                values[5] = dgDTO.Email;
                name[6] = "@MaLDG";
                values[6] = dgDTO.MaLDG;
                name[7] = "@NgayLap";
                values[7] = dgDTO.NgayLap;
                name[8] = "@NgayHetHan";
                values[8] = dgDTO.NgayHetHan;
                return connect.UpdateData("sp_DocGia_Insert", name, values, Nparamater);

            }
            catch (SqlException ex)
            {
                throw ex;
            }

        }
        // sửa thông tin độc giả
        public int UpdateDG_MaDG(DocGiaDTO dgDTO)
        {
            connect = new ConnectionData();
            try
            {
                //var binding = BindingObject(dgDTO);
                // return connect.UpdateData("sp_DocGia_Insert", binding.Names, binding.Values, binding.Names.Length);
                int Nparamater = 9;
                String[] name = new string[Nparamater];
                object[] values = new object[Nparamater];
                name[0] = "@MaDG";
                values[0] = dgDTO.MaDG;
                name[1] = "@HoVaTen";
                values[1] = dgDTO.HoVaTen;
                name[2] = "@GioiTinh";
                values[2] = dgDTO.GioiTinh;
                name[3] = "@NgaySinh";
                values[3] = dgDTO.NgaySinh;
                name[4] = "@DiaChi";
                values[4] = dgDTO.DiaChi;
                name[5] = "@Email";
                values[5] = dgDTO.Email;
                name[6] = "@MaLDG";
                values[6] = dgDTO.MaLDG;
                name[7] = "@NgayLap";
                values[7] = dgDTO.NgayLap;
                name[8] = "@NgayHetHan";
                values[8] = dgDTO.NgayHetHan;
                return connect.UpdateData("DocGia_Update_GetByMaDG", name, values, Nparamater);

            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
        public DataTable TimDG_MaDG(DocGiaDTO dgDTO)
        {
            try
            {
                connect = new ConnectionData();
                int Nparamater = 1;
                string[] name = new string[Nparamater];
                object[] values = new object[Nparamater];
                name[0] = "@MaDG";
                values[0] = dgDTO.MaDG;
                return connect.GetData("FindDocGia_GetByMaDG", name, values, Nparamater);

            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
        public DataTable TimDG_HoTen(DocGiaDTO dgDTO)
        {
            try
            {
                connect = new ConnectionData();
                int Nparamater = 1;
                string[] name = new string[Nparamater];
                object[] values = new object[Nparamater];
                name[0] = "@HoVaTen";
                values[0] = dgDTO.HoVaTen;
                return connect.GetData("FindDocGia_GetByHoTen", name, values, Nparamater);

            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
        // tim doc gia theo loai doc gia
        /*public DataTable TimDG_LoaiDG(DocGiaDTO dgDTO) {
            try {
                connect = new ConnectionData();
                int Nparamater = 1;
                string[] name = new string[Nparamater];
                object[] values = new object[Nparamater];
                name[0] = "@loaidocgia";
                values[0] = dgDTO.MaLDG;
                return connect.GetData("FindDocGia_GetByLoaiDG",name,values,Nparamater);

            }catch(SqlException ex){
                throw ex;
            }
        }*/
        // tim doc gia theo ten
        public DataTable DocGia_TimTheoTen(DocGiaDTO dgDTO)
        {
            try
            {
                connect = new ConnectionData();
                int Nparamater = 1;
                string[] name = new string[Nparamater];
                object[] value = new object[Nparamater];
                name[0] = "@search";
                value[0] = dgDTO.MaDG;
                return connect.GetData("DocGia_TimTheoTen", name, value, Nparamater);
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
        // load độc giả by mả độc giả
        public DataTable LoadDG_MaDG(DocGiaDTO dgDTO)
        {
            try
            {
                connect = new ConnectionData();
                int Nparamater = 1;
                string[] name = new string[Nparamater];
                object[] values = new object[Nparamater];
                name[0] = "@MaDG";
                values[0] = dgDTO.MaDG;
                return connect.GetData("Load_DocGia_GetByMaDG", name, values, Nparamater);
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
        // Tim doc gia theo the loai
        public DataTable LoadDG_TimTheoLoaiDG(DocGiaDTO dgDTO) {
            try
            {
                connect = new ConnectionData();
                int Nparamater = 1;
                string[] name=new string[Nparamater];
                object[] values=new object[Nparamater];
                name[0] = "@loaidocgia";
                values[0] = dgDTO.MaLDG;
                return connect.GetData("DocGia_TimTheoLoaiDG",name,values,Nparamater);
            }
            catch(SqlException ex) {
                throw ex;
            }
        }

    }
}
